#!/bin/bash

function scandir() {
    local cur_dir parent_dir workdir
    workdir=$1
    cd ${workdir}
    if [ ${workdir} = "/" ]
    then
        cur_dir=""
    else
        cur_dir=$(pwd)
    fi

    dir_array=$(ls -A ${cur_dir})

    for dirlist in $dir_array
    do
        if test -d ${dirlist};then
        	 if [ "${dirlist}" == ".git" ];then
				echo "******this is a git responsy"
				result=$(git remote -v)
				echo $result
				if [[ $result == *"10.52.1.122"* ]];then
					echo "发现目标！！！！！！"
					echo $(pwd)
					dele_left=${result#*origin}
					dele_right="${dele_left%(fetch)*}"
					old_url=${dele_right//' '/''}
					new_url=${old_url//'10.52.1.122'/'10.52.1.169'}
					echo "old:$old_url"
					#设置新url
					echo "new:$new_url"
					$(git remote set-url origin $new_url)
				fi
			fi
            cd ${dirlist}
            scandir ${cur_dir}/${dirlist}
            cd ..
        # else
        #     echo ${cur_dir}/${dirlist}
        fi
    done
}

if test -d $1
then
	if [ $1 = '']; then
		echo "命令后 请 跟上绝对路径"
		exit
	fi
    scandir $1
elif test -f $1
then
    echo "you input a file but not a directory,pls reinput and try again"
    exit 1
else
    echo "the Directory isn't exist which you input,pls input a new one!!"
    exit 1
fi
